package com.micrometa.km.ui.components.tree;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.micrometa.km.util.FileUtil;

public class FileTreeNodeUtil {
	public static FileTreeNode getFileTreeNodes(File f) {
		FileTreeNode parent = new FileTreeNode();
		parent.setFile(f);
		if (f.isDirectory()) {
			if (f != null && f.canRead()) {
				for (File child : f.listFiles()) {
					if (FileUtil.filter(child)) {
						FileTreeNode node = getFileTreeNodes(child);
						node.setFile(child);
						parent.add(node);
					}
				}
			}
		}
		return parent;
	}

	/**
	 * get FileTreeNode from file dir, and dir is root
	 * @param dir
	 * @return
	 */
	public static FileTreeNode getFileTreeNodesParentSub(File dir) {
		FileTreeNode parent = new FileTreeNode();
		parent.setFile(dir);
		if (dir != null && dir.isDirectory() && dir.canRead()) {
			for (File child : dir.listFiles()) {
				if (FileUtil.filter(child)) {
					FileTreeNode node = new FileTreeNode();
					node.setFile(child);
					parent.add(node);
				}
			}
		}
		return parent;
	}
	/**
	 * get FileTreeNodes of file dir
	 * @param dir
	 * @return
	 */
	public static List<FileTreeNode> getFileTreeNodesSub(File dir){
		List<FileTreeNode> ln=new ArrayList<FileTreeNode>();
		if (dir != null && dir.isDirectory() && dir.canRead()) {
			for (File child : dir.listFiles()) {
				if (FileUtil.filter(child)) {
					FileTreeNode node = new FileTreeNode();
					node.setFile(child);
					ln.add(node);
				}
			}
		}
		return ln;
	}
}
